package cn.virens.common.project.base.utils;

import cn.virens.common.mybatis.entity.BaseModel;
import cn.virens.common.util.core.objcet.ObjectUtil;
import cn.virens.common.util.exception.APIException;
import org.dromara.hutool.core.text.placeholder.StrFormatter;

import java.util.Map;

public class MapConverUtil {

    /**
     * 根据模板生成对应的字符串
     *
     * @param tpl   模板
     * @param model 对象
     */
    public static String format(String tpl, Object model) throws APIException {
        return StrFormatter.formatByBean(tpl, map(model), false);
    }

    public static Map<?, ?> map(Object model) throws APIException {
        if (model instanceof BaseModel) return toMap((BaseModel) model);
        if (model instanceof Map) return (Map<?, ?>) model;

        return ObjectUtil.toMap(model, "参数为空");
    }

    public static Map<String, Object> toMap(BaseModel model) {
        return toMap(model, "对象为空");
    }

    public static Map<String, Object> toMap(BaseModel model, String err) {
        Map<String, Object> answer = ObjectUtil.toMap(model, err);
        answer.put("createTime", model.getCreateTime());
        answer.put("updateTime", model.getUpdateTime());
        answer.put("id", model.getId());

        return answer;
    }
}
